﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Simple.Entity.Entity;
using Simple.IRepository;
using Simple.Tools.SnowFlake;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Simple.Repository
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public class WholeRepository<TEntity,Key>:BaseRepository<TEntity,Key>,IWholeRepository<TEntity,Key> where TEntity : WholeEntity<Key>,new()
	{


		public WholeRepository()
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="entity"></param>
		/// <param name="CreatorId"></param>
		/// <returns></returns>
		public async  Task<bool> CreateAsync(TEntity entity, long CreatorId)
		{
			SetAdd(entity, CreatorId);
			return await base.CreateAsync(entity);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="entitys"></param>
		/// <param name="CreatorId"></param>
		/// <returns></returns>
		public async Task<bool> CreateAsync(List<TEntity> entitys, long CreatorId)
		{
			foreach (var entity in entitys)
			{
				SetAdd(entity, CreatorId);
			}
			return await base.CreateAsync(entitys);
		}
		/// <summary>
		/// 返回添加之后的实体
		/// </summary>
		/// <param name="entity"></param>
		/// <param name="CreatorId"></param>
		/// <returns></returns>

		public async Task<TEntity> CreateReturnEntityAsync(TEntity entity, long CreatorId)
		{
			SetAdd(entity, CreatorId);
			return await base.CreateReturnEntityAsync(entity);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="lamada"></param>
		/// <param name="deleterId"></param>
		/// <returns></returns>

		public async Task<bool> SoftDelete(Expression<Func<TEntity, bool>> lamada, long deleterId)
		{
			var entities = await dbClient.Queryable<TEntity>().Where(lamada).ToListAsync();
			foreach (var entity in entities)
			{
				SetSoftDelete(entity, deleterId);
			}
			return await dbClient.Updateable(entities).ExecuteCommandAsync() > 0;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="entity"></param>
		/// <param name="updateId"></param>
		/// <returns></returns>
		public async Task<bool> UpdateAsync(TEntity entity, long updateId)
		{
			SetUpdate(entity, updateId);
			return await base.UpdateAsync(entity);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="entitys"></param>
		/// <param name="updateId"></param>
		/// <returns></returns>
		public async Task<bool> UpdateAsync(List<TEntity> entitys, long updateId)
		{
			foreach (var entity in entitys)
			{
				SetUpdate(entity, updateId);
			}
			return await base.UpdateAsync(entitys);
		}

		/// <summary>
		/// 更新需要设置实体的值
		/// </summary>
		/// <param name="entity"></param>
		/// <param name="updateid"></param>
		private void SetUpdate(TEntity entity, long updateid)
		{
			entity.ConcurrencyStamp = Worker.GetId();
			entity.LastModificationTime = DateTime.Now;
			entity.LastModifierID = updateid;
		}
		/// <summary>
		/// 添加需要设置实体的值
		/// </summary>
		/// <param name="entity"></param>
		/// <param name="CreatorId"></param>
		private void SetAdd(TEntity entity, long CreatorId)
		{
			entity.CreationTime = DateTime.Now;
			entity.LastModificationTime = DateTime.Now;
			entity.CreatorID = CreatorId;
			entity.ConcurrencyStamp = Worker.GetId();
			entity.LastModifierID = CreatorId;
		}
		/// <summary>
		/// 软删除需要设置实体的值
		/// </summary>
		/// <param name="entity"></param>
		/// <param name="updateId"></param>
		private void SetSoftDelete(TEntity entity, long updateId)
		{
			entity.ConcurrencyStamp = Worker.GetId();
			entity.DeletionTime = DateTime.Now;
			entity.IsDelete = entity.IsDelete ? false : true;
			entity.DeleterID = updateId;
		}
	}
}
